package us.icitap.cm.ui.shared;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.VerticalLayout;

import us.icitap.cm.business.entities.Alias;
import us.icitap.cm.business.entities.Item;
import us.icitap.cm.business.entities.Report;
import us.icitap.cm.business.entities.ReportRestriction;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustButton;
import us.icitap.cm.ui.custom.CustDateField;
import us.icitap.cm.ui.custom.CustTable;
import us.icitap.cm.ui.custom.CustTextField;
import us.icitap.cm.ui.custom.CustomFormLayout;

public class ReportRestrictionField extends CustomField<List<ReportRestriction>>{

	private BeanItemContainer<ReportRestriction> beans = new BeanItemContainer<ReportRestriction>(ReportRestriction.class);
	
	
	private CustTable restTable = new  CustTable();
	
	private CustTextField usernameTextField = new CustTextField("Perdoruesi");
	
	private CheckBox noLimitCheckBox = new CheckBox("Pa limit kohor");
	
	private CustDateField fromDateField = new CustDateField("Nga data");
	
	private CustDateField toDateField = new CustDateField("Deri me daten");
	
	
	
	CustomFormLayout cfl = new CustomFormLayout("500px", "Perdoruesit me te drejta", "Reset", "Shto");
	
	private final CustBeanFieldGroup<ReportRestriction> binder = new CustBeanFieldGroup<ReportRestriction>(ReportRestriction.class);
	
	private Report report;
	ReportRestriction reportRestriction = new ReportRestriction();
	
	public ReportRestrictionField(){
		
		
		
	}
	
	public void setReport(Report report){
		this.report = report;
		reportRestriction.setReport(report);
		binder.setItemDataSource(reportRestriction);
	}
	public void _init() {
		this.usernameTextField.setWidth("150px");

	}
	
	
	
	@Override
	protected Component initContent() {
		
		VerticalLayout vl = new VerticalLayout();
		
		restTable.setContainerDataSource(beans);
		restTable.setVisibleColumns(new Object[]{ "username", "noLimit", "fromDate", "toDate", "active", "deactivationDate"});
		restTable.setColumnHeaders(new String[]{ "Perdoruesi", "Pa limit", "Nga data", "Deri", "Aktive", "Dt. c'aktivizimit"});
		restTable.setPageLength(5);
		restTable.setImmediate(true);
		
		vl.addComponent(restTable);
		
		
		FormLayout fl = new FormLayout();
	
		fl.addComponent(this.usernameTextField);
		fl.addComponent(this.noLimitCheckBox);
		fl.addComponent(this.fromDateField);
		fl.addComponent(this.toDateField);
		
		vl.addComponent(fl);
		
		
		binder.bind(this.usernameTextField, "username");
		binder.bind(this.noLimitCheckBox, "noLimit");
		binder.bind(this.fromDateField, "fromDate");
		binder.bind(this.toDateField, "toDate");
		
		
		cfl.addComponent(vl);
		cfl.addFooter();
		cfl.addRightClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				
				try {
					binder.commit();
					reportRestriction.setRecordDate(new Date());
					beans.addBean(reportRestriction);
					
					
					
					if(ReportRestrictionField.this.getInternalValue() == null){
						ReportRestrictionField.this.setInternalValue(new ArrayList<ReportRestriction>());
					}
					
					
					List<ReportRestriction> newValue = new ArrayList<ReportRestriction>(ReportRestrictionField.this.getInternalValue());
					
					newValue.add(reportRestriction);
					
					ReportRestrictionField.this.setInternalValue(newValue);
					
					
					reportRestriction = new ReportRestriction();
					reportRestriction.setReport(report);
					binder.setItemDataSource(reportRestriction);
					
				} catch (CommitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		return cfl;
	}

	@Override
	public Class<? extends List<ReportRestriction>> getType() {
		return  (Class<? extends List<ReportRestriction>>) List.class;
	}
	
	@Override
	protected void setInternalValue(List<ReportRestriction> newValue) {
		
		super.setInternalValue(newValue);
		if(newValue == null){
			return;
		}
		
		if(newValue != null && !newValue.isEmpty()){
			this.beans.removeAllItems();
			beans.addAll(newValue);
		}else
			this.beans.removeAllItems();

			
	}

}

